<?php

class Profile extends Controller {

	function index()
	{
		$this->load->view('head', array());
		$this->load->view('foot');
	}

	function change_password()
	{
		if(config_item('structure') == 'demo')
		{
			return;
		}
		$error = '';
		if(isset($_POST['password1']) && isset($_POST['password2']))
		{
			$pass1 = trim($_POST['password1']);
			$pass2 = trim($_POST['password2']);
			if($pass1 == $pass2)
			{
				$arr = array('password' => md5($pass1));
				$user_id = Auth::$user_id;
				$this->MUser->save($arr, $user_id);
				$error = 'Пароль изменён';
				Auth::rememberUser($user_id, $arr['password']);
			}
			else
			{
				$error = 'Пароли не совпадают';
			}
		}
		$this->load->view('head', array());
		$this->load->view('profile/change_password', array('error' => $error));
		$this->load->view('foot');
	}

	function logout()
	{
		Auth::logout();
		redirect(".");
	}

	function setting()
	{
		global $VALUTA, $TIME_ZONES, $ZNAK_DROB;
		$this->load->model('MSetting');
		$error = '';
		$setting = $this->MSetting->getMe();
		$me = $this->MUser->get(Auth::$user_id);
		if(@$_POST['valuta'])
		{
			$arr = array(
				'format_kro' => trim(@$_POST['format_kro']),
				'valuta' => trim(@$_POST['valuta']),
				'time_zone' => trim(@$_POST['time_zone']),
				'znak_drob' => trim(@$_POST['znak_drob']),
				'send_email_more' => intval(@$_POST['send_email_more']) ? intval(@$_POST['send_email_more']) : null,
				'user_id' => Auth::$user_id
			);
			if(!isset($VALUTA[$arr['valuta']])) $arr['valuta'] = 'R';
			if(!isset($TIME_ZONES[$arr['time_zone']])) $arr['time_zone'] = MOSCOW_TIME_ZONE;
			if(!isset($ZNAK_DROB[$arr['znak_drob']])) $arr['znak_drob'] = DEFAULT_ZNAK_DROB;
			$this->MSetting->save($arr, @$setting['id']);
			$error = 'Ваши настройки обновлены';
		}
		$setting = $this->MSetting->getMe();

		$this->load->view('head', array());
		$this->load->view('profile/setting', array('setting' => $setting, 'error' => $error, 'me' => $me));
		$this->load->view('foot');
	}



}
?>